<template>
	<view>
		<cu-custom bgColor="bg-gradual-green" :isBack="true" zIndex="6">
			<block slot="backText"></block>
			<block slot="content">海报生成</block>
		</cu-custom>
		<wm-poster v-on:onErr="onErr" v-on:success='success' :Referrer="info.name + '推荐您'" :PostId="info.post_id" :imgSrc="info.img" Width="750"
		 :Title="info.content"></wm-poster>
		<view class="padding" v-if="code">
			<button class="cu-btn bg-gradual-green lg block" v-if="openSettingBtnHidden" @click="saveEwm">保存海报</button>
			<button class="cu-btn bg-gradual-green lg block" v-else hover-class="none" open-type="openSetting" @opensetting='handleSetting'>保存海报</button>
		</view>
	</view>

</template>

<script>
	import wmPoster from "@/components/wm-poster/wm-poster.vue"
	export default {
		components: {
			wmPoster
		},
		data() {
			return {
				info: '',
				code: '',
				isButton: false,
				openSettingBtnHidden: true, //是否授权
			}
		},

		mounted() {},
		onLoad() {

		},
		onShow() {
			let info = uni.getStorageSync('codeInfo');
			this.info = {
				content: info.content,
				img: info.imageArray[0],
				name: info.poster.nickname,
				avatar: info.poster.avatar,
				post_id: info.post_id
			}
		},
		methods: {
			success(res) {
				this.isButton = true;
				this.code = res.tempFilePath;
				console.log(res.tempFilePath)
			},
			onErr(err){
				console.log(err)
				this.isButton = true;
			},
			handleSetting(e) {
				if (!e.detail.authSetting['scope.writePhotosAlbum']) {
					this.openSettingBtnHidden = false;
				} else {
					this.openSettingBtnHidden = true;
				}
			},
			saveEwm: function(e) {
				let _self = this;
				//获取相册授权
				uni.getSetting({
					success(res) {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
									//这里是用户同意授权后的回调
									_self.saveImgToLocal();
								},
								fail() { //这里是用户拒绝授权后的回调
									_self.openSettingBtnHidden = false
								}
							})
						} else { //用户已经授权过了
							_self.saveImgToLocal();
						}
					}
				})
			},
			saveImgToLocal: function(e) {
				let _self = this;
				uni.showModal({
					title: '提示',
					content: '确定保存到相册吗',
					success: function(res) {
						if (res.confirm) {

							uni.saveImageToPhotosAlbum({
								filePath: _self.code,
								success: function() {
									uni.showToast({
										title: "保存成功",
									});
									
									setTimeout(()=>{
										uni.navigateBack({
											delta:1
										})
									},1000)
								},
								fail: function() {
									uni.showToast({
										title: "保存失败",
										icon: "none"
									});
								}
							});

						} else if (res.cancel) {

						}
					}
				});
			}
		}
	}
</script>

<style scoped lang="less">
</style>
